﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Public
{
	public partial class AddTitleBlocksForm : System.Windows.Forms.Form
	{
		public AddTitleBlocksForm(Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			this.doc = doc;
			this.GetUserSetting();
			this.GetName(doc.ActiveView.Id);
		}

		public AddTitleBlocksForm(Autodesk.Revit.DB.Document doc, ViewSheet vs)
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			this.doc = doc;
			if (vs != null)
			{
				this.initType = "Edit";
				this.GetName(vs.Id);
				this.InitType();
				this.Text = "编辑图纸";
				this.sheetNum = vs.SheetNumber;
				this.sheetName = vs.Name;
				this.txtName.Text = this.sheetName;
				this.txtNum.Text = this.sheetNum;
			}
			else
			{
				this.initType = "New";
				this.txtName.Text = "未命名";
				this.txtNum.Text = "未命名";
				this.Text = "新建图纸";
			}
			this.isManage = true;
			this.txtName.Enabled = true;
			this.txtNum.Enabled = true;
			this.btnLay.Text = "确定";
		}

		private void InitType()
		{
			this.tabSize = 0;
			this.tabWidth = 0;
			this.sheetHeight = 0.0;
			this.sheetWidth = 0.0;
			if (this.titleBlock != null)
			{
				Parameter parameter = this.titleBlock.GetParameter(BuiltInParameter.SHEET_HEIGHT);
				if (parameter != null)
				{
					this.sheetHeight = parameter.AsDouble() * 304.8;
				}
				Parameter parameter2 = this.titleBlock.GetParameter(BuiltInParameter.SHEET_WIDTH);
				if (parameter2 != null)
				{
					this.sheetWidth = parameter2.AsDouble() * 304.8;
				}
				if (Math.Abs(this.sheetHeight - 841.0) < 1.0)
				{
					this.tabSize = 0;
					this.tabWidth = this.CalSheetWidthType(this.sheetWidth, 1189.0);
				}
				else if (Math.Abs(this.sheetHeight - 594.0) < 1.0)
				{
					this.tabSize = 1;
					this.tabWidth = this.CalSheetWidthType(this.sheetWidth, 841.0);
				}
				else if (Math.Abs(this.sheetHeight - 420.0) < 1.0)
				{
					this.tabSize = 2;
					this.tabWidth = this.CalSheetWidthType(this.sheetWidth, 594.0);
				}
				else if (Math.Abs(this.sheetHeight - 297.0) < 1.0)
				{
					this.tabSize = 3;
					this.tabWidth = this.CalSheetWidthType(this.sheetWidth, 420.0);
				}
				else
				{
					YJKMessageBox.Information("图框尺寸不规则，设置为默认尺寸");
				}
				this.SetSheetType(this.tabSize, this.tabWidth);
			}
		}

		private int CalSheetWidthType(double width, double standard)
		{
			int result = 0;
			if (Math.Abs(standard - width) / standard < 0.1)
			{
				result = 0;
			}
			else if (Math.Abs(standard - width) / standard < 0.3)
			{
				result = 1;
			}
			else if (Math.Abs(standard - width) / standard < 0.6)
			{
				result = 2;
			}
			else if (Math.Abs(standard - width) / standard < 0.8)
			{
				result = 3;
			}
			else if (Math.Abs(standard - width) / standard < 1.1)
			{
				result = 4;
			}
			else if (Math.Abs(standard - width) / standard >= 1.1)
			{
				result = 5;
			}
			return result;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private bool CheckChange(int atbSize, int atbWidth)
		{
			bool flag = false;
			bool flag2 = false;
			switch (atbSize)
			{
			case 0:
				flag = this.rdoA0.Checked;
				break;
			case 1:
				flag = this.rdoA1.Checked;
				break;
			case 2:
				flag = this.rdoA2.Checked;
				break;
			case 3:
				flag = this.rdoA3.Checked;
				break;
			}
			switch (atbWidth)
			{
			case 0:
				flag2 = this.rdoZero.Checked;
				break;
			case 1:
				flag2 = this.rdoOnefourths.Checked;
				break;
			case 2:
				flag2 = this.rdoHalf.Checked;
				break;
			case 3:
				flag2 = this.rdoThreefourths.Checked;
				break;
			case 4:
				flag2 = this.rdoOne.Checked;
				break;
			case 5:
				flag2 = this.rdoFivefourths.Checked;
				break;
			}
			return !flag || !flag2;
		}

		private void btnLay_Click(object sender, EventArgs e)
		{
			if (this.txtName.Text.Trim() == "")
			{
				YJKMessageBox.Information("图纸名称不能为空");
				this.txtName.Focus();
				return;
			}
			if (this.txtNum.Text.Trim() == "")
			{
				YJKMessageBox.Information("图纸编号不能为空");
				this.txtNum.Focus();
				return;
			}
			if (this.initType == "Edit" || this.initType == "New")
			{
				foreach (Element elem in YJKElementFilter.GetElementByCategory(this.doc, BuiltInCategory.OST_Sheets))
				{
					Parameter parameter = elem.GetParameter(BuiltInParameter.SHEET_NUMBER);
					if (parameter != null && parameter.AsString() == this.txtNum.Text.Trim() && this.sheetNum != parameter.AsString())
					{
						YJKMessageBox.Information("图纸编号不能重复");
						this.txtNum.Focus();
						return;
					}
				}
				this.sheetName = this.txtName.Text.Trim();
				this.sheetNum = this.txtNum.Text.Trim();
				if (this.initType == "New" || (this.CheckChange(this.tabSize, this.tabWidth) && this.initType == "Edit") || this.titleBlock == null)
				{
					this.LoadFamily();
					this.fs = this.GetFamilySymbol();
				}
			}
			else
			{
				this.LoadFamily();
				this.fs = this.GetFamilySymbol();
				this.UserSettingWrite();
			}
			base.DialogResult = DialogResult.OK;
		}

		private FamilySymbol GetFamilySymbol()
		{
			FamilySymbol result = null;
			if (this.rdoA0.Checked)
			{
				if (this.rdoZero.Checked)
				{
					result = this.fsName("A0");
				}
				else if (this.rdoOnefourths.Checked)
				{
					result = this.fsName("A0+1/4");
				}
				else if (this.rdoHalf.Checked)
				{
					result = this.fsName("A0+1/2");
				}
				else if (this.rdoThreefourths.Checked)
				{
					result = this.fsName("A0+3/4");
				}
				else if (this.rdoOne.Checked)
				{
					result = this.fsName("A0+1");
				}
				else if (this.rdoFivefourths.Checked)
				{
					result = this.fsName("A0+5/4");
				}
			}
			if (this.rdoA1.Checked)
			{
				if (this.rdoZero.Checked)
				{
					result = this.fsName("A1");
				}
				else if (this.rdoOnefourths.Checked)
				{
					result = this.fsName("A1+1/4");
				}
				else if (this.rdoHalf.Checked)
				{
					result = this.fsName("A1+1/2");
				}
				else if (this.rdoThreefourths.Checked)
				{
					result = this.fsName("A1+3/4");
				}
				else if (this.rdoOne.Checked)
				{
					result = this.fsName("A1+1");
				}
				else if (this.rdoFivefourths.Checked)
				{
					result = this.fsName("A1+5/4");
				}
			}
			if (this.rdoA2.Checked)
			{
				if (this.rdoZero.Checked)
				{
					result = this.fsName("A2");
				}
				else if (this.rdoOnefourths.Checked)
				{
					result = this.fsName("A2+1/4");
				}
				else if (this.rdoHalf.Checked)
				{
					result = this.fsName("A2+1/2");
				}
				else if (this.rdoThreefourths.Checked)
				{
					result = this.fsName("A2+3/4");
				}
				else if (this.rdoOne.Checked)
				{
					result = this.fsName("A2+1");
				}
				else if (this.rdoFivefourths.Checked)
				{
					result = this.fsName("A2+5/4");
				}
			}
			if (this.rdoA3.Checked)
			{
				if (this.rdoZero.Checked)
				{
					result = this.fsName("A3");
				}
				else if (this.rdoOnefourths.Checked)
				{
					result = this.fsName("A3+1/4");
				}
				else if (this.rdoHalf.Checked)
				{
					result = this.fsName("A3+1/2");
				}
				else if (this.rdoThreefourths.Checked)
				{
					result = this.fsName("A3+3/4");
				}
				else if (this.rdoOne.Checked)
				{
					result = this.fsName("A3+1");
				}
				else if (this.rdoFivefourths.Checked)
				{
					result = this.fsName("A3+5/4");
				}
			}
			return result;
		}

		private void LoadFamily()
		{
			bool flag = false;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.doc);
			filteredElementCollector.OfClass(typeof(Family)).ToElements();
			IEnumerator<Element> enumerator = filteredElementCollector.GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (((Family)enumerator.Current).Name.CompareTo("HY-图框（横版）") == 0)
					{
						flag = true;
						break;
					}
				}
			}
			if (!flag)
			{
				string versionNumber = this.doc.Application.VersionNumber;
				string text = Path.Combine(Product.FamilyLocation, versionNumber, "PictureFrame", "HyFram.rft");
				SubTransaction subTransaction = new SubTransaction(this.doc);
				subTransaction.Start();
				this.doc.LoadFamily(text);
				subTransaction.Commit();
			}
		}

		private FamilySymbol fsName(string familyName)
		{
			List<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(FamilySymbol), this.doc);
			FamilySymbol result = null;
			if (elementByClass.Count <= 0)
			{
				return null;
			}
			foreach (Element element in elementByClass)
			{
				FamilySymbol familySymbol = element as FamilySymbol;
				if (familySymbol != null && familySymbol.Name == familyName)
				{
					result = familySymbol;
					break;
				}
			}
			return result;
		}

		private void GetName(ElementId viewId)
		{
			using (List<Element>.Enumerator enumerator = new FilteredElementCollector(this.doc, viewId).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_TitleBlocks).ToElements().ToList<Element>().GetEnumerator())
			{
				if (enumerator.MoveNext())
				{
					Element element = enumerator.Current;
					this.titleBlock = element;
				}
			}
			foreach (Element element2 in new FilteredElementCollector(this.doc).OfClass(typeof(ViewSheet)).OfCategory(BuiltInCategory.OST_Sheets).ToElements().ToList<Element>())
			{
				if (element2.Id == this.doc.ActiveView.Id)
				{
					this.txtName.Text = element2.GetParameter(BuiltInParameter.SHEET_NAME).AsString();
					this.txtNum.Text = element2.GetParameter(BuiltInParameter.SHEET_NUMBER).AsString();
					break;
				}
			}
		}

		private void GetUserSetting()
		{
			AddTitleBlocksSetParam addTitleBlocksText = new HVACUserSetting().GetAddTitleBlocksText();
			this.SetSheetType(addTitleBlocksText.Size, addTitleBlocksText.Width);
		}

		private void SetSheetType(int atbSize, int atbWidth)
		{
			switch (atbSize)
			{
			case 0:
				this.rdoA0.Checked = true;
				break;
			case 1:
				this.rdoA1.Checked = true;
				break;
			case 2:
				this.rdoA2.Checked = true;
				break;
			case 3:
				this.rdoA3.Checked = true;
				break;
			}
			switch (atbWidth)
			{
			case 0:
				this.rdoZero.Checked = true;
				return;
			case 1:
				this.rdoOnefourths.Checked = true;
				return;
			case 2:
				this.rdoHalf.Checked = true;
				return;
			case 3:
				this.rdoThreefourths.Checked = true;
				return;
			case 4:
				this.rdoOne.Checked = true;
				return;
			case 5:
				this.rdoFivefourths.Checked = true;
				return;
			default:
				return;
			}
		}

		private void UserSettingWrite()
		{
			HVACUserSetting hvacuserSetting = new HVACUserSetting();
			AddTitleBlocksSetParam addTitleBlocksSetParam = new AddTitleBlocksSetParam();
			if (this.rdoA0.Checked)
			{
				addTitleBlocksSetParam.Size = 0;
			}
			if (this.rdoA1.Checked)
			{
				addTitleBlocksSetParam.Size = 1;
			}
			if (this.rdoA2.Checked)
			{
				addTitleBlocksSetParam.Size = 2;
			}
			if (this.rdoA3.Checked)
			{
				addTitleBlocksSetParam.Size = 3;
			}
			if (this.rdoZero.Checked)
			{
				addTitleBlocksSetParam.Width = 0;
			}
			if (this.rdoOnefourths.Checked)
			{
				addTitleBlocksSetParam.Width = 1;
			}
			if (this.rdoHalf.Checked)
			{
				addTitleBlocksSetParam.Width = 2;
			}
			if (this.rdoThreefourths.Checked)
			{
				addTitleBlocksSetParam.Width = 3;
			}
			if (this.rdoOne.Checked)
			{
				addTitleBlocksSetParam.Width = 4;
			}
			if (this.rdoFivefourths.Checked)
			{
				addTitleBlocksSetParam.Width = 5;
			}
			hvacuserSetting.WriteAddTitleBlocksText(addTitleBlocksSetParam);
		}

		private void txtName_Validated(object sender, EventArgs e)
		{
			foreach (char value in this.enchars)
			{
				if (this.txtName.Text.ToCharArray().Contains(value))
				{
					YJKMessageBox.Warning("图纸名称中不能包含下列任一字符：\n \\ : { } [ ] | ; < > ? ` ~ \n也不能包含任何不可打印的字符。");
					this.txtName.Undo();
					this.txtName.Focus();
				}
			}
		}

		private void txtNum_Validated(object sender, EventArgs e)
		{
			foreach (char value in this.enchars)
			{
				if (this.txtNum.Text.ToCharArray().Contains(value))
				{
					YJKMessageBox.Warning("图纸编号中不能包含下列任一字符：\n \\ : { } [ ] | ; < > ? ` ~ \n也不能包含任何不可打印的字符。");
					this.txtNum.Undo();
					this.txtNum.Focus();
				}
			}
		}

		private Document doc;

		public FamilySymbol fs;

		public string sheetName = "";

		public string sheetNum = "";

		public double sheetHeight;

		public double sheetWidth;

		private int tabSize;

		private int tabWidth;

		private bool isManage;

		private string initType = "";

		public Element titleBlock;

		private char[] enchars = new char[]
		{
			'\\',
			':',
			'{',
			'}',
			'[',
			']',
			'|',
			'<',
			'>',
			';',
			'?',
			'`',
			'~'
		};
	}
}
